using Microsoft.AspNetCore.Mvc;
using ProcessManagementApi.DTO;
using ProcessManagementApi.Services;
using MESData.ErrorCode;

namespace ProcessManagementApi.Controllers
{
    /// <summary>
    /// 工序分类管理控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class ProcessCategoryController : ControllerBase
    {
        private readonly IProcessCategoryService _processCategoryService;

        public ProcessCategoryController(IProcessCategoryService processCategoryService)
        {
            _processCategoryService = processCategoryService;
        }

        /// <summary>
        /// 分页查询工序分类
        /// </summary>
        /// <param name="queryDto">查询条件</param>
        /// <returns>分页结果</returns>
        [HttpGet]
        [Route("page")]
        public async Task<ApiPageing<ProcessCategoryResultDto>> GetProcessCategories([FromQuery] ProcessCategoryQueryDto queryDto)
        {
            return await _processCategoryService.GetProcessCategoriesAsync(queryDto);
        }

        /// <summary>
        /// 根据ID获取工序分类详情
        /// </summary>
        /// <param name="id">工序分类ID</param>
        /// <returns>工序分类详情</returns>
        [HttpGet]
        [Route("{id}")]
        public async Task<ApiResult<ProcessCategoryResultDto>> GetProcessCategoryById(int id)
        {
            return await _processCategoryService.GetProcessCategoryByIdAsync(id);
        }

        /// <summary>
        /// 创建工序分类
        /// </summary>
        /// <param name="createDto">创建DTO</param>
        /// <returns>创建结果</returns>
        [HttpPost]
        public async Task<ApiResult<ProcessCategoryResultDto>> CreateProcessCategory([FromBody] ProcessCategoryCreateDto createDto)
        {
            return await _processCategoryService.CreateProcessCategoryAsync(createDto);
        }

        /// <summary>
        /// 更新工序分类
        /// </summary>
        /// <param name="updateDto">更新DTO</param>
        /// <returns>更新结果</returns>
        [HttpPut]
        public async Task<ApiResult<ProcessCategoryResultDto>> UpdateProcessCategory([FromBody] ProcessCategoryUpdateDto updateDto)
        {
            return await _processCategoryService.UpdateProcessCategoryAsync(updateDto);
        }

        /// <summary>
        /// 删除工序分类
        /// </summary>
        /// <param name="id">工序分类ID</param>
        /// <returns>删除结果</returns>
        [HttpDelete]
        [Route("{id}")]
        public async Task<ApiResult<bool>> DeleteProcessCategory(int id)
        {
            return await _processCategoryService.DeleteProcessCategoryAsync(id);
        }

        /// <summary>
        /// 获取所有启用的工序分类（用于下拉选择）
        /// </summary>
        /// <returns>工序分类列表</returns>
        [HttpGet]
        [Route("active")]
        public async Task<ApiResult<List<ProcessCategoryResultDto>>> GetActiveProcessCategories()
        {
            return await _processCategoryService.GetActiveProcessCategoriesAsync();
        }

        /// <summary>
        /// 导出工序分类到Excel
        /// </summary>
        /// <param name="queryDto">查询条件</param>
        /// <returns>Excel文件</returns>
        [HttpGet]
        [Route("export")]
        public async Task<IActionResult> ExportProcessCategories([FromQuery] ProcessCategoryExportQueryDto queryDto)
        {
            try
            {
                var excelData = await _processCategoryService.ExportProcessCategoriesToExcelAsync(queryDto);
                
                var fileName = $"工序分类导出_{DateTime.Now:yyyyMMddHHmmss}.xlsx";
                
                return File(excelData, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName);
            }
            catch (Exception ex)
            {
                return BadRequest(new { message = $"导出失败：{ex.Message}" });
            }
        }
    }
} 